home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: usinternet.com!not-for-mail
- From: "Luke J. Bucklin" <lbucklin@mail.usinternet.com>
- Subject: Re: [] overload..(newbie in distress)
- Message-ID: <11a7cc$d2619.341@usinternet.com>
- Date: Fri, 26 Jan 1996 19:38:25 GMT
- Organization: Management Computer Solutions
- X-Mailer: Mozilla 1.22 (Windows; U; 16bit)
- MIME-Version: 1.0
- References: <DLGppJ.31C@undergrad.math.uwaterloo.ca> <4e0ui5$kjn@newsroom.hitc.com>
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
-
-
- psand@eos.hitc.com (G. Patrick Sand) wrote:
- >In article <DLGppJ.31C@undergrad.math.uwaterloo.ca>,
- >tthiraku@landen.math.uwaterloo.ca says...
- >>
- >>Hi..
- >>
- >>I'm currently stuck on how to overload [] operator such that
- >>it does two different tasks..
- >>
- >>
- >>case 1:
- >>
- >>// A is an instance of a class that contains a linkist.
- >>
- >> A[5] = val; // store val into the fifth node of a linklist.
- >> val = A[5] ; // returns the value of the fifth node of a linklist.
- >>
- >>
- >>I was wondering how can C++ differentiate these two senerios?
- >>
- >>Anything help will greatly appreciated..
- >>
- >>Thank You Kindly
- >>--
- >>Thanou Thirakul
- >>tthiraku@undergrad.math.uwaterloo.ca
- >>http://www.undergrad.math.uwaterloo.ca/~tthiraku
-
- Have the operator return a reference. Then, since the reference is being
- used, the = can be on either side:
-
- Type &operator[](Valtype Val);
-
- This should allow you to do
-
- A[5] = val;
- as well as
- val = A[5]
-
- because A[5] is actually a reference to the "return value".
-
- ------------------------------------
- Luke J. Bucklin
- Management Computer Solutions, Inc.
- lbucklin@usinternet.com
- ------------------------------------
-
-
-
-